#include "../Helix/View/OpenGL/OpenGLSupport.h"

#include "../Helix/Helix.h"
#include "../Helix/Application.h"
#include "../Helix/Window.h"
#include "../Helix/Input.h"
#include "../Helix/DirectInput.h"
#include "../Helix/Timer.h"
#include "../Helix/HighPerformanceTimer.h"
#include "Game.h"					// Header File For Derived Application Class
#include <math.h>

using namespace helix;

int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	MSG		msg;									// Windows Message Structure
	bool	fullScreen=true;						// Bool Variable To start in fullscreen

	// Ask The User Which Screen Mode They Prefer
	if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
		fullScreen=false;							// Windowed Mode
	}

	//get an instance of the Helix application
	Application *app=Application::getInstance();

	//set the log file to output to
	app->Log().setLog("helix.log");
	
	//initialize the application by specifying a window type and setting the window parameters
	if(!app->initApp(Application::OPEN_GL_WINDOW,"Helix",800,600,32,45.0f,fullScreen))
	{
		app->Log().Error("Initialization of the application failed.");
			//deinitialize everything
		app->destroyApp();
		return (msg.wParam);
	}


	app->Log().Println("Window Created, starting main game loop.");

	//create a new input object

	Timer *timer;
	Input *input;
	Window *wind;

	Game *game;
	

	timer=(Timer*) new HighPerformanceTimer();
	wind=app->window();
	input=(Input*) new DirectInput();
	((DirectInput*)input)->initialize(wind->getHINSTANCE(),wind->HWND());

	game=new Game();

	app->setTimer(timer);
	app->setInput(input);
	app->setRenderer(game);
	app->setUpdater(game);

	game->initialize();

	//Disable vsync
	app->window()->getRenderHelper()->setVSync(false);

	//run the application
	while(app->runMainLoop(&msg));

	app->Log().Println("Game loop ended.  Shutting down Helix application...");

	//deinitialize everything
	app->destroyApp();

	return (msg.wParam);
}